package org.steven.crm.app.api;

import com.steven.common.utils.StringUtil;

import java.text.SimpleDateFormat;
import java.util.*;

public class AppUtil {
	public static String getImageURL(String dirName, String src, int type) {
		if (src.startsWith("http")) {
			return src;
		}
		
		String image;
		if (src != null && !src.equals("")) {
			src = src.replace("\\", "/");
			String srcActual = src;
			String subDirName = "";
			int index = src.lastIndexOf("/");
			
			if (index != -1) {
				subDirName = src.substring(0, index + 1);
				srcActual = src.substring(index + 1);
			}
			
			if (type != 0) {
				image = "/uploadFile/" + subDirName + type + "_" + srcActual;
			}
			else {
				image = "/uploadFile/" + subDirName + srcActual;
			}
		} else {
			image = "/uploadFile/none.jpg";
			if (dirName.equals("shop")) {
				image = "/uploadFile/resource/default_shop.jpg";
			}
		}
		
		return (AppKeys.DOMAIN_IMAGE.startsWith("http") ? AppKeys.DOMAIN_IMAGE : ("http://" + AppKeys.DOMAIN_IMAGE)) + image;
	}
	
	public static String getProductImage(Map<String, String> product, int type) {
		String image = product.get("mainImage");
		if (image == null) {
			image = product.get("image");
		}
		
		return getImageURL("product", image, type);
	}
	
	public static String splitString(String str, int count) throws Exception {
		int reInt = 0;
		String reStr = "";
		if (str == null) {
			return "";
		}
	
		char[] tempChar = str.toCharArray();
		int kk = 0;
		for (; (kk < tempChar.length && count > reInt); kk++) {
			String s1 = String.valueOf(tempChar[kk]);
			byte[] b = s1.getBytes("GBK");
			reInt += b.length;
			reStr += tempChar[kk];
		}
		return reStr + (kk == (tempChar.length) ? "" : "...");
	}
	
	/**
	 * 将data中的指定的字段值进行特殊字符转换处理
	 * @param columns 需要进行特殊字符转换的字段
	 * @param data 需要转换的数据
	 * @throws Exception
	 */
	public static void convertToHtml(String[] columns, 
			Map<String, String> data) throws Exception {
		for (int i = 0; i < columns.length; ++i) {
			if (data.get(columns[i]) != null && !data.get(columns[i]).equals("")) {
				data.put(columns[i], StringUtil.convertXmlChars(data.get(columns[i])));
			}
		}
	}
	
	public static void convertToHtml(String[] columns, 
			List<Map<String, String>> datas) throws Exception {
		int size = datas.size();
		for (int j = 0; j < size; ++j) {
			Map<String, String> data = datas.get(j);
			convertToHtml(columns, data);
		}
	}
	
	public static String parseTimeToMills(String dateTime) throws Exception {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date date = sdf.parse(dateTime);
		return String.valueOf(date.getTime());
	}
	
	public static String getDirName(String fileName) {
		int index = fileName.lastIndexOf("/");
		if (index != -1) {
			return fileName.substring(0, index);
		}
		
		return "";
	}
	
	public static String getFileName(String fileName) {
		int index = fileName.lastIndexOf("/");
		if (index != -1 && ((index + 1) < fileName.length())) {
			return fileName.substring(index + 1);
		}
		
		return "";
	}
	
	public static String dealLink(String link) {
		if (!link.equals("#") && !link.equals("") && !link.startsWith("/") 
				&& !link.startsWith("http:") && !link.startsWith("https:") && !link.startsWith("javascript")) {
			return "http://" + link;
		} else {
			return link;
		}
	}

	public static Map<String, String> getArea(String townID) {
		Map<String, String> area = new HashMap<String, String>();
		area.put("townName", "");
		area.put("townId", "");
		area.put("cityName", "");
		area.put("cityId", "");
		area.put("provinceName", "");
		area.put("provinceId", "");
		Map<String, String> town = LocalDataCache.getInstance().getTown(townID);
		if (!town.isEmpty()) {
			area.put("townName", town.get("name"));
			area.put("townId", town.get("id"));
			String cityId = town.get("pid");
			Map<String, String> city = LocalDataCache.getInstance().getCity(cityId);
			if (!city.isEmpty()) {
				area.put("cityName", city.get("name"));
				area.put("cityId", city.get("id"));
				String provinceId = city.get("pid");
				Map<String, String> province = LocalDataCache.getInstance().getProvince(provinceId);
				if (!province.isEmpty()) {
					area.put("provinceName", province.get("name"));
					area.put("provinceId", province.get("id"));
				}
			}
		}

		area.put("areaIds", area.get("provinceId") + "," + area.get("cityId") + "," + area.get("townId"));
		area.put("name", area.get("provinceName") + "/" + area.get("cityName") + "/" + area.get("townName"));

		return area;
	}

	public static String getDateBefore(String date, String format, int type, int num)
			throws  Exception {
		Calendar now = Calendar.getInstance();
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
		now.setTime(simpleDateFormat.parse(date));
		now.set(type, now.get(type) + num);
		return simpleDateFormat.format(now.getTime());
	}
}
